<template>
  <div class="survey container-content">
    <div class="app-view-box">
      <el-row>
        <el-col :span="24">
          <h1 class="panel-title">采购合同</h1>
        </el-col>
      </el-row>
      <el-divider class="divider"></el-divider>
      <el-row class="export-container">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addNewData"
          >新增</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-download"
          size="mini"
          @click="exporting"
          >导出</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-edit-outline"
          size="mini"
          @click="changeContract"
          >合同变更</el-button
        >
      </el-row>
      <el-table
        ref="customerTable"
        :data="tableData"
        stripe
        style="width: 100%"
        border
        @selection-change="handleSelectionChange"
        :summary-method="getSummaries"
        show-summary
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column
          type="index"
          width="55"
          label="序号"
          align="center"
        ></el-table-column>
        <el-table-column prop="materialCode" label="物料编码" width="100">
          <template slot-scope="scope">
            {{ scope.row.materialCode || "--" }}
          </template>
        </el-table-column>
        <el-table-column prop="materialName" label="物料名称" width="100">
          <template slot-scope="scope">
            {{ scope.row.materialName || "--" }}
          </template>
        </el-table-column>
        <el-table-column prop="contractNumber" label="合同编号" width="150">
          <template slot-scope="scope">
            {{ scope.row.contractNumber || "--" }}
          </template>
        </el-table-column>
        <el-table-column prop="contractName" label="合同名称" width="100">
          <template slot-scope="scope">
            {{ scope.row.contractName || "--" }}
          </template>
        </el-table-column>
        <el-table-column prop="number" label="合同数量" width="100">
          <template slot-scope="scope">
            {{ scope.row.number || "--" }}
          </template>
        </el-table-column>
        <el-table-column prop="money" label="合同金额" width="100">
          <template slot-scope="scope">
            {{ scope.row.money || "--" }}
          </template>
        </el-table-column>
        <el-table-column prop="moneyUpper" label="合同金额大写" width="150">
          <template slot-scope="scope">
            {{ scope.row.moneyUpper || "--" }}
          </template>
        </el-table-column>
        <el-table-column prop="type" label="合同类型" width="100">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.type == 1" type="success">采购</el-tag>
            <el-tag v-else-if="scope.row.type == 2" type="primary">行政</el-tag>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column prop="payType" label="付款方式" width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.payType">{{ scope.row.payType }}</span>
            <span v-else>--</span>
          </template>
        </el-table-column>

        <el-table-column label="联系人职务" width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.contactPosition">{{
              scope.row.contactPosition
            }}</span>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column label="关联合同模板" width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.contractTempId">{{
              scope.row.contractTempId
            }}</span>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column label="邮箱" width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.email">{{ scope.row.email }}</span>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column label="汇率" width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.rate">{{ scope.row.rate }}</span>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column label="本位币" width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.standardCurrency">{{
              scope.row.standardCurrency
            }}</span>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column label="供货方" width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.supplierGoodsId">{{
              scope.row.supplierGoodsId
            }}</span>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column label="供货方联系人" width="150">
          <template slot-scope="scope">
            <span v-if="scope.row.supplierContactId">{{
              scope.row.supplierContactId
            }}</span>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column label="供货方地址" width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.supplierAddress">{{
              scope.row.supplierAddress
            }}</span>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column label="供应商名称" width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.supplierName">{{
              scope.row.supplierName
            }}</span>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column label="联系人手机号" width="150">
          <template slot-scope="scope">
            <span v-if="scope.row.telPhone">{{ scope.row.telPhone }}</span>
            <span v-else>--</span>
          </template>
        </el-table-column>

        <el-table-column prop="projectType" label="公司项目类型" width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.projectType">{{
              scope.row.projectType
            }}</span>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column prop="remarks" label="备注" width="100">
          <template slot-scope="scope">
            {{ scope.row.remarks || "--" }}
          </template>
        </el-table-column>
        <el-table-column prop="signTime" label="签订日期" width="150">
          <template slot-scope="scope">
            {{ scope.row.signTime || "--" }}
          </template>
        </el-table-column>
        <el-table-column prop="state" label="状态" width="100">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.state == '-1'" type="danger">暂存</el-tag>
            <el-tag v-else-if="scope.row.state == '0'" type="success"
              >审核中</el-tag
            >
            <el-tag v-else-if="scope.row.state == '2'" type="success"
              >已审核</el-tag
            >
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <!-- <el-tooltip
                            class="item"
                            effect="dark"
                            content="详情"
                            placement="bottom"
                        >
                            <el-button
                                plain
                                icon="el-icon-more"
                                @click="handleDetail(scope.$index, scope.row)"
                                size="mini"
                            ></el-button>
                        </el-tooltip> -->
            <el-tooltip
              class="item"
              effect="dark"
              content="提交"
              placement="bottom"
            >
              <el-button
                :disabled="scope.row.state !== '-1'"
                type="success"
                plain
                icon="el-icon-s-promotion"
                @click="handleSubmit(scope.$index, scope.row)"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="dark"
              content="编辑"
              placement="bottom"
            >
              <el-button
                type="primary"
                plain
                icon="el-icon-edit"
                @click="handleEdit(scope.$index, scope.row)"
                size="mini"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="dark"
              content="删除"
              placement="bottom"
            >
              <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                @click="handleDelete(scope.$index, scope.row)"
                size="mini"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-pagination
      class="table-pagination"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      background
    >
    </el-pagination>
    <ContractDialog
      :previewVisible.sync="openDialog"
      :contractTempContent="contractContent"
    />
    <!-- 弹出框 -->
    <el-drawer
      :title="getDialogTitle"
      :visible.sync="drawer"
      direction="rtl"
      :before-close="handleClose"
      size="100%"
      destroy-on-close
    >
      <div class="drawer-content">
        <FormCom ref="formRef" :id="rowId" :change="change" />
      </div>
      <div class="demo-drawer__footer">
        <el-button @click="cancelForm">取 消</el-button>
        <el-button type="primary" @click="submit">保存</el-button>
      </div>
    </el-drawer>
  </div>
</template>
<script>
import { getmenuToModel } from "@/api/model";
import ContractDialog from "@/views/contract/portal/dialog/index.vue";
import {
  getProcureList,
  deleteProcureList,
  submitProcure, //采购提交
} from "@/api/system/contract";
import FormCom from "./add/index";
export default {
  components: {
    ContractDialog,
    FormCom,
  },
  data() {
    return {
      searchValue: "",
      total: 0,
      tableData: [],
      openDialog: false,
      contractContent: "",
      drawer: false, //弹窗显示
      rowId: "",
      showDetail: false,
      selectItem: "",
      change: false,
      limit: 10,
      page: 1,
    };
  },
  computed: {
    getDialogTitle() {
      if (this.rowId) {
        if (this.change) {
          return "变更采购合同";
        } else {
          return "编辑采购合同";
        }
      } else {
        return "新增采购合同";
      }
    },
  },
  watch: {
    "$store.state.workflow.businessDrawer": {
      handler(val) {
        if (!val) {
          this.getData();
        }
      },
    },
  },
  methods: {
    // 提交
    handleSubmit(index, row) {
      getmenuToModel(this.$route.meta.menuId).then((res) => {
        submitProcure({
          id: row.id,
          businessType: "8",
          modelId: res.data.id,
        }).then((res) => {
          this.$message.success("提交成功");
          this.getData();
        });
      });
    },
    //分页器size
    handleSizeChange(val) {
      this.limit = val;
      this.getData();
    },
    //分页器当前页
    handleCurrentChange(val) {
      this.page = val;
      this.getData();
    },
    addNewData() {},
    exporting() {},
    // 获取列表请求
    getData() {
      let params = {
        limit: this.limit,
        page: this.page,
        type: 1,
      };
      getProcureList(params).then((res) => {
        if (res.code === 200) {
          this.tableData = res.data.records;
          this.total = res.data.total;
        }
      });
    },
    // 删除事件
    handleDelete(index, row) {
      this.$confirm("请确定要删除所选的记录吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteProcureList(row.id).then((res) => {
            if (res.code === 200) {
              this.tableData.splice(index, 1);
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 合同变更
    changeContract() {
      if (this.selectItem.length !== 1) {
        this.$message.error("请选择一条数据！");
      } else {
        // console.log("this.selectItem", this.selectItem);
        this.rowId = this.selectItem[0].id;
        localStorage.setItem("changeid", this.rowId);
        // this.drawer = true;
        // this.change = true;
        this.$store.dispatch("workflow/getModel", {
          menuId: this.$route.meta.menuId,
          type: "launch",
          businessType: "8",
        });
      }
    },
    // 搜索事件
    searchData() {
      // let searchValue = this.searchValue;
      // searchSurvey({ searchValue }).then((res) => {
      //   this.tableData = res.rows;
      //   console.log("请求成功", res);
      // });
    },
    //新增按钮
    addNewData() {
      // this.drawer = true;
      // this.rowId = "";
      this.$store.dispatch("workflow/getModel", {
        menuId: this.$route.meta.menuId,
        type: "launch",
        businessType: "8",
      });
    },
    //编辑按钮
    handleEdit(index, row) {
      this.change = false;
      this.rowId = row.id;
      this.drawer = true;
    },
    // 详情按钮
    handleDetail(index, row) {
      this.openDialog = true;
      this.contractContent = row.contractContent;
    },
    // 保存
    submit() {
      const cb = () => {
        this.drawer = false;
        this.getData();
      };
      this.$refs.formRef.onSubmit(cb);
    },
    // 取消
    cancelForm() {
      this.drawer = false;
      this.rowId = "";
      this.getData();
    },
    // 关闭 drawer
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
          this.rowId = "";
          this.getData();
        })
        .catch((_) => {});
    },
    handleSelectionChange(val) {
      this.selectItem = val;
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "总计";
          return;
        }
        if (column.property === "number") {
          const values = data.map((item) => Number(item[column.property]));
          if (!values.every((value) => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return prev + curr;
              } else {
                return prev;
              }
            }, 0);
            sums[index] += "";
          } else {
            sums[index] = "N/A";
          }
        } else {
          return;
        }
      });

      return sums;
    },
  },
  created() {
    this.getData();
  },
};
</script>
<style lang="scss" scoped>
.divider {
  margin: 0;
}

.export-container {
  margin: 15px 0;
}

.pagination {
  margin-top: 10px;
}
</style>
